package com.example.linktop.chatuidemo;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by linktoplinktop on 2017/6/27.
 */
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {

    private List<ChatMessage> msgList;

    public MsgAdapter(List<ChatMessage> msgList) {
        this.msgList = msgList;
    }

    static class ViewHolder extends   RecyclerView.ViewHolder {
        LinearLayout leftLayout, rightLayout;
        TextView leftTxt, rightTxt;

        private int type;

        public void setType(int type, String content) {
            this.type = type;
            if (this.type == ChatMessage.MSG_TYPE_RECEIVE) { //left show
                leftLayout.setVisibility(View.VISIBLE);
                rightLayout.setVisibility(View.INVISIBLE);
                leftTxt.setText(content);
                rightTxt.setText(null);

            } else  {
                leftLayout.setVisibility(View.INVISIBLE); //right show
                rightLayout.setVisibility(View.VISIBLE);
                leftTxt.setText(null);
                rightTxt.setText(content);
            }

        }

        public ViewHolder(View itemView) {
            super(itemView);
            leftLayout = (LinearLayout) itemView.findViewById(R.id.msgLinearLeft);
            rightLayout = (LinearLayout)itemView.findViewById(R.id.msgLinearRight);
            leftTxt = (TextView)itemView.findViewById(R.id.msgTxtLeft);
            rightTxt = (TextView)itemView.findViewById(R.id.msgTxtRight);
        }
    }


    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //生成实例
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_itme, parent, false);

        return new ViewHolder(view);
    }




    @Override
    public int getItemCount() {
        return msgList.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //渲染
        ChatMessage tmpMsg = msgList.get(position);

        holder.setType(tmpMsg.getChatType(), tmpMsg.getContentString()); //设置对其方式


    }
}
